package com.czd.support.controller;

import com.czd.base.model.JsonResult;
import com.czd.support.common.Constants;
import com.czd.support.model.Manager;
import com.czd.support.model.MessageLog;
import com.czd.support.service.MessageLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * Author: Jerry.hu
 * Create: Jerry.hu (2014-10-04 22:42)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/message")
public class MessageController extends BaseController {

    @Autowired
    private MessageLogService messageLogService;

    /**
     * 发送消息
     * @param messageLog
     * @param request
     * @return
     */
    @RequestMapping(value = "/send",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult sendMsg(MessageLog messageLog,HttpServletRequest request){
        Manager manager = this.getSesionUser(request);
        messageLog.setCreater(manager.getUserCode());
        if(StringUtils.isBlank(messageLog.getRoomId())){
            messageLog.setType(Constants.MSG_GLOBAL_CODE);
        }else if(messageLog.getRoomId().split(Constants.BATCH_SPLIT).length > 1){
            messageLog.setType(Constants.MSG_GROUPS_CODE);
        }else {
            messageLog.setType(Constants.MSG_SINGLE_CODE);
        }
        if(messageLogService.insert(messageLog)){
            return JsonResult.OK;
        }
        return JsonResult.FAIL;
    }
}
